package com.hrbxlh.actionImpl;
import java.math.BigDecimal;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.hrbxlh.dao.CourseDao;
import com.hrbxlh.virtualization.service.impl.CourseServiceImpl;
import com.vladsch.flexmark.Extension;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.ParserEmulationProfile;
import com.vladsch.flexmark.util.options.MutableDataSet;
import org.apache.commons.io.FileUtils;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.*;

import com.hrbxlh.pojo.Chapter;
import com.hrbxlh.pojo.ExpreLibrary;
import com.hrbxlh.pojo.TaskLibrary;
import com.hrbxlh.util.*;
import com.hrbxlh.vo.ExpreLibraryVO;
import com.hrbxlh.vo.TaskLibraryVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hrbxlh.dao.ChapterDao;
import com.hrbxlh.dao.HomeworkTestDao;

/**
 * -------------------------------------------------------
 * Copyright (c) 2017, 鑫联华信息科技股份有限公司
 * All rights reserved.
 * FileName：ChapterActionImpl.java
 * Description：章节actionImpl类,负责操作章节数据以及逻辑处理。
 * History：
 * Date           Author               Desc
 * 2017.3.27		  xzx                 建立
 * -------------------------------------------------------
 */

@Component("chapterActionImpl")
@Transactional
public class ChapterActionImpl<T> {

	@Resource(name="chapterDao")
	private ChapterDao chapterDao;

	@Resource(name="homeworkTestDao")
	private HomeworkTestDao homeworkTestDao;

	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;

	@Resource(name="courseDao")
	private CourseDao courseDao;

	@Resource(name = "courseServiceImpl")
	private CourseServiceImpl courseServiceImpl;

	/*ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-mybatis.xml");
	ChapterActionImpl chapterActionImpl = (ChapterActionImpl)app.getBean("punchClockService");*/


	/**
	 * @description  添加章节上半部分
	 * @param chapterName addTestMark
	 * @param response
	 * @return
	 */
	@Transactional
	public Map<String, Object> addChapterTopOne(Map<String, Object> paramMap, HttpServletRequest req) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			//查询是否有这个名字的章节
//			List<Map<String, Object>> chapterList= chapterDao.getChapterByName(paramMap);
//			if(chapterList == null || chapterList.size() == 0){
				//添加工号
				paramMap.put("stuorteaId", new SessionValues().getSessionValues(new String[]{"stuorteaId"}, req, "userMsg").get("stuorteaId")) ;
				//添加
				chapterDao.addChapterTopOne(paramMap);
				int chapterId = Integer.parseInt(String.valueOf(paramMap.get("chapterId")));
				//将章节编号加入到章节表中
				String courseMark = "ZDY"+"•"+String.valueOf(chapterId);
				resultMap.put("courseMark",courseMark);
                resultMap.put("chapterId", chapterId);
				courseDao.updatChapterCousrMark(resultMap);
				resultMap.put("result", "success");
				resultMap.put("failMsg", "");
			/*}else{
				resultMap.put("result", "fail");
				resultMap.put("failMsg", "章节名称已存在");
			}*/
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加章节之---课件
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapterCourseWare(Map<String, Object> paramMap, HttpServletRequest req) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			//添加
			boolean flag = chapterDao.addChapterCourseWare(paramMap);
			resultMap.put("flag",flag);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加章节之---备课材料or实验手册
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapterMaterialOrTest(Map<String, Object> paramMap,HttpServletRequest request) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			//markdow的基本配置
			MutableDataSet options = new MutableDataSet();
			options.setFrom(ParserEmulationProfile.MARKDOWN);
			options.set(Parser.EXTENSIONS, Arrays.asList(new Extension[] { TablesExtension.create()}));
			Parser parser = Parser.builder(options).build();
			HtmlRenderer renderer = HtmlRenderer.builder(options).build();
			//获取章节基本信息
			paramMap.put("coursemark",1);
			Map<String,Object> chapterMap = chapterDao.getOneChapter(paramMap);
			String chapterName = String.valueOf(chapterMap.get("chapterName"));
			//课程标识
			String courseMark = String.valueOf(chapterMap.get("courseMark"));
			//生成的文件名
			String filName = chapterName +courseMark;
			String html = "";
			//传过来的markdown
			String htmlStr = "";
			//将mrakdown转换为html文件
			if(Integer.parseInt(String.valueOf(paramMap.get("mark")))==1){
				//备课材料
				//先解码
				//20180913将新传来的html代码写入文件
//				htmlStr = Escape.unescape(String.valueOf(paramMap.get("courseMaterialNameHtml")));
				filName = filName+"-备课材料.html";
				paramMap.put("courseMaterialName",filName);


			}if(Integer.parseInt(String.valueOf(paramMap.get("mark")))==2){
				//实验手册
				//先解码
				//20180913将新传来的html代码写入文件
//				htmlStr = Escape.unescape(String.valueOf(paramMap.get("testNameHtml")));
				filName = filName+"-实验手册.html";
				paramMap.put("testDocName",filName);
			}
			htmlStr = String.valueOf(paramMap.get("htmlStr"));
			//测试用的
//				String path = "C://Users/Administrator/Desktop/xxbg/upload/report/";
			String[] keysArray = {"tomcatUrl", "project_name","server_address"};
			Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String path = String.valueOf(urlmap.get("tomcatUrl"))+String.valueOf(urlmap.get("project_name"))+"upload/course/";
			String filePath = path+filName;
			PubMethods.creatHtml(htmlStr,filePath,request);
			//添加
			boolean flag = chapterDao.addChapterMaterialOrTest(paramMap);
			resultMap.put("flag",flag);
			resultMap.put("filename",filName);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加章节之---课程材料
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapterPackageFile(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			//添加
			boolean flag = chapterDao.addChapterPackageFile(paramMap);
			resultMap.put("flag",flag);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加章节之---视频
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapterVideo(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			//添加
			boolean flag = chapterDao.addChapterVideo(paramMap);
			resultMap.put("flag",flag);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加章节--作业
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapterHomework(Map<String, Object> paramMap,CommonsMultipartFile homeworkexcel,int mark) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			if(mark==2){
				//是修改，判断当前章节是否已经有学习记录
				paramMap.put("taskType",1);
				boolean flag = getStudyHistory(paramMap);
				if(!flag){
					resultMap.put("result",false);
					resultMap.put("errMsg","存在学习记录不可修改");
					return resultMap;
				}

			}
			//获取表格中数据
			resultMap = getDate(homeworkexcel,null,Integer.parseInt(String.valueOf(paramMap.get("chapterId"))));
			if(resultMap==null||resultMap.size()==0){
				resultMap.put("result",true);
				resultMap.put("errMsg","");
			}

		} catch (Exception e) {
			resultMap.put("result", false);
			resultMap.put("errMsg", "数据填写有问题");
			throw e;
		}
		return resultMap;
	}


	/**
	 * 查看是否有学习记录
	 */
	private boolean getStudyHistory(Map<String,Object> paramMap){
		try {
			List<Map<String,Object>> homeworkOrExpreList = chapterDao.getHomeworkListByChapterId(paramMap);
			if(homeworkOrExpreList!=null&&homeworkOrExpreList.size()!=0){
				return false;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * @description 添加章节--实验报告
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapterExpreexcel(Map<String, Object> paramMap,CommonsMultipartFile expreexcel,int mark) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			if(mark==2){
				//是修改，判断当前章节是否已经有学习记录
				paramMap.put("taskType",2);
				boolean flag = getStudyHistory(paramMap);
				if(!flag){
					resultMap.put("result",false);
					resultMap.put("errMsg","存在学习记录不可修改");
					return resultMap;
				}

			}
			//获取表格中数据
			resultMap = getDate(null,expreexcel,Integer.parseInt(String.valueOf(paramMap.get("chapterId"))));
			if(resultMap==null||resultMap.size()==0){
				resultMap.put("result",true);
				resultMap.put("errMsg","");
			}

		} catch (Exception e) {
			resultMap.put("result", false);
			resultMap.put("errMsg", "数据填写有问题");
			throw e;
		}
		return resultMap;
	}

	/**
	 * @description 添加章节
	 * @param paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	@Transactional
	public Map<String, Object> addChapter(Map<String, Object> paramMap, HttpServletRequest req,CommonsMultipartFile homeworkexcel,CommonsMultipartFile expreexcel) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			//查询是否有这个名字的章节
			List<Map<String, Object>> chapterList= chapterDao.getChapterByName(paramMap);
			if(chapterList == null || chapterList.size() == 0){
				//添加工号
				paramMap.put("stuorteaId", new SessionValues().getSessionValues(new String[]{"stuorteaId"}, req, "userMsg").get("stuorteaId")) ;
				//将参数放到Chapter对象里
				Chapter chapter = getParam(paramMap);
				//添加
				chapterDao.addChapter(chapter);
				int newCharpterId = chapter.getChapterId();
				//获取表格中数据
				resultMap = getDate(homeworkexcel,expreexcel,newCharpterId);
				if(resultMap==null||resultMap.size()==0){
					resultMap.put("result",true);
				}
				resultMap.put("newCharpterId",newCharpterId);
			}else{
				resultMap.put("result", false);
				resultMap.put("errMsg", "课节名称已存在");
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	public Chapter getParam(Map<String,Object> paramMap){
		Chapter chapter = new Chapter();
		chapter.setPackageFile(coverStr(paramMap.get("packageFile")));
		chapter.setTestNameHtml(coverStr(paramMap.get("testNameHtml")));
		chapter.setTestDocName(coverStr(paramMap.get("testDocName")));
		chapter.setStuorteaId(coverStr(paramMap.get("stuorteaId")));
		chapter.setChapterName(coverStr(paramMap.get("chapterName")));
		chapter.setTestAddress(coverStr(paramMap.get("testAddress")));
		chapter.setCourseAnswerName(coverStr(paramMap.get("courseAnswerName")));
		chapter.setCourseAnswerNameHtml(coverStr(paramMap.get("courseAnswerNameHtml")));
		chapter.setCoursewareName(coverStr(paramMap.get("coursewareName")));
		chapter.setCoursewareNameHtml(coverStr(paramMap.get("coursewareNameHtml")));
		chapter.setVideoName(coverStr(paramMap.get("videoName")));
		chapter.setHomeworkName(coverStr(paramMap.get("homeworkName")));
		chapter.setHomeworkNameHtml(coverStr(paramMap.get("homeworkNameHtml")));
		chapter.setType(Integer.valueOf(coverStr(paramMap.get("type"))));
		chapter.setAddTestMark(Integer.valueOf(coverStr(paramMap.get("addTestMark"))));
		chapter.setHomeworkNameDoc(coverStr(paramMap.get("homeworkNameDoc")));
		chapter.setCourseMaterialName(coverStr(paramMap.get("courseMaterialName")));
		chapter.setCourseMaterialNameHtml(coverStr(paramMap.get("courseMaterialNameHtml")));
		chapter.setExpreHtml(coverStr(paramMap.get("expreHtml")));
		chapter.setTaskHtml(coverStr(paramMap.get("taskHtml")));
		chapter.setHomworkPaperDetail(coverStr(paramMap.get("homworkPaperDetail")));
		chapter.setExprementPaperDetail(coverStr(paramMap.get("exprementPaperDetail")));
		chapter.setActualSumSource(new BigDecimal(coverStr(paramMap.get("actualSumSource"))));
		chapter.setActualTestSumSource(new BigDecimal(coverStr(paramMap.get("actualTestSumSource"))));
		chapter.setCourseMark(coverStr(paramMap.get("courseMark")));
		chapter.setExpreName(coverStr(paramMap.get("expreName")));
		chapter.setExpreNameHtml(coverStr(paramMap.get("expreNameHtml")));
		chapter.setHomeworkModelName(coverStr(paramMap.get("homeworkModelName")));
		return chapter;
	}

	//将null转换成""
	private String coverStr(Object obj){
		String str = "";
		if(obj instanceof String){
			str = obj==null?"":String.valueOf(obj);
		}if(obj instanceof Integer){
			str = obj==null?"0":String.valueOf(obj);
		}if(obj instanceof BigDecimal){
			str = obj==null?"0":String.valueOf(obj);
		}
		return str;
	}

	/**
	 * 获取实验报告或作业数据
	 */
	public Map<String,Object> getDate(CommonsMultipartFile homeworkexcel,CommonsMultipartFile expreexcel,int newCharpterId) throws Exception {
		String homeworkName = "";
		String expreName = "";
		Map<String, Object> finalMap = new HashMap<>();
		if(homeworkexcel!=null){
			 homeworkName = homeworkexcel.getOriginalFilename();
		}if(expreexcel!=null){
			expreName = expreexcel.getOriginalFilename();
		}
		Map<String, Object> paramMap = new HashMap<>();
		Map<String, Object> resultMap = new HashMap<>();
		if (!homeworkName.equals("") && homeworkName != null) {
			//将excel作业数据插入到数据库
			List<String> params = Arrays.asList("tasklibrary_name", "tasklibrary_typeName", "tasklibrary_optionMsg", "tasklibrary_answer","tasklibrary_knowpoint", "tasklibrary_source");
			finalMap = imHomeWorkExcel(homeworkexcel, (Class<T>) TaskLibrary.class, params, 3);
			List<TaskLibrary> taskLibraryList = (List<TaskLibrary>) finalMap.get("list");
			if (taskLibraryList != null && (String.valueOf(finalMap.get("msg")).equals("")||finalMap.get("msg")==null)) {
				//上传作业文件模板名
				String homeworkModelName = homeworkexcel.getOriginalFilename().substring(0,homeworkexcel.getOriginalFilename().lastIndexOf("."));
				String ext = homeworkexcel.getOriginalFilename().substring(homeworkexcel.getOriginalFilename().lastIndexOf("."),homeworkexcel.getOriginalFilename().length());
				//查询章节信息
				paramMap.put("coursemark",1);
				paramMap.put("chapterId",newCharpterId);
				Map<String,Object> chapterMap = chapterDao.getOneChapter(paramMap);
				String courseMark = String.valueOf(chapterMap.get("courseMark"));
				homeworkModelName = homeworkModelName+"•"+courseMark+ext;
				//将上传作业文件模板保存到服务器
				//判断文件夹存不存在,不存在就创建
				String[] keysArray = {"tomcatUrl", "project_name","server_address"};
				Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
				String path = String.valueOf(urlmap.get("tomcatUrl"))+String.valueOf(urlmap.get("project_name"))+"upload/homeworkModel/"+homeworkModelName;
//				String path = "C:\\Users\\Administrator\\Desktop\\xxbg\\course\\"+homeworkModelName;
				File saveFile = new File(path);
				if (!saveFile.exists()) {
					String s = path.substring(0,path.lastIndexOf("/") + 1);
					File saveFileDir = new File(s);
					saveFileDir.mkdirs();
				}
				File destFile = new File(path);
				FileUtils.copyInputStreamToFile(homeworkexcel.getInputStream(),destFile);// 复制临时文件到指定目录下
				boolean flag = addTaskOrExpreLibrary((List<T>) taskLibraryList, 1, newCharpterId,homeworkModelName,courseMark);
				if (!flag) {
					resultMap.put("result", false);
					resultMap.put("errMsg", finalMap.get("操场失败"));
					//手动进行事务回滚
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}
				resultMap.put("actualSumSource",finalMap.get("actualSumSource"));
				resultMap.put("result", flag);
			} else {
				resultMap.put("result", false);
				resultMap.put("errMsg", finalMap.get("msg"));
				//手动进行事务回滚
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return resultMap;
			}
		}
		/*if (!expreName.equals("") && expreName != null) {
			//将excel实验数据插入到数据库
			List<String> params = Arrays.asList("exprelibrary_name", "exprelibrary_objective", "exprelibrary_content", "exprelibrary_step", "exprelibrary_result", "exprelibrary_thinkproblem", "exprelibrary_source");
			finalMap = imHomeWorkExcel(expreexcel, (Class<T>) ExpreLibrary.class, params, 3);
			List<ExpreLibrary> expreLibraryList = (List<ExpreLibrary>) finalMap.get("list");
			if (expreLibraryList != null && (String.valueOf(finalMap.get("msg")).equals("")||finalMap.get("msg")==null)) {
				boolean flag = addTaskOrExpreLibrary((List<T>) expreLibraryList, 2, newCharpterId);
				if (!flag) {
					//手动进行事务回滚
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}
				resultMap.put("actualSumSource",finalMap.get("actualSumSource"));
				resultMap.put("result", flag);
			} else {
				resultMap.put("result", false);
				resultMap.put("errMsg", finalMap.get("msg"));
				//手动进行事务回滚
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return resultMap;
			}
		}*/
		if(Boolean.parseBoolean(String.valueOf(resultMap.get("result")))){
			//说明成功存放数据了，需要将文件名插入到章节表中
			paramMap.put("homeworkName",homeworkName);
			paramMap.put("expreName",expreName);
			paramMap.put("chapterId",newCharpterId);
			paramMap.put("actualSumSource",finalMap.get("actualSumSource"));
			//将实际总分数插入到chapter表中
			chapterDao.addChapterHomeworkOrExprement(paramMap);
		}
		return resultMap;
	}

	/**
	 * 将作业or实验报告的数据导入数据库中
	 * @param file
	 * @param Class clazz
	 * @param list<String> strList 字段名称
	 * @param 起始行
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> imHomeWorkExcel(CommonsMultipartFile file,Class<T> clazz,List<String> listStr,int startLine) throws Exception {
		PubImExcel pubImExcel = new PubImExcel();
		String fileName = file.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		Map<String,Object> finalMap = new HashMap<String, Object>();

		try {
			Workbook wb = null;
			if (".xls".equals(suffix)) {// 2003 读取Excel
				wb = new HSSFWorkbook(file.getInputStream());
				finalMap =pubImExcel.readXlsOrXlsxFile(listStr, file, wb, startLine, clazz);
			} else if (".xlsx".equals(suffix)) {// 2007
				wb = new XSSFWorkbook(file.getInputStream());
				finalMap = pubImExcel.readXlsOrXlsxFile(listStr, file, wb, startLine, clazz);
			}else {
				return null;
			}
		} catch (Exception e) {
//			logger.error("文件读取失败！", e);
		}
		return finalMap;
	}

	/**
	 * @description 获取某课程的章节信息
	 * @param  paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getCourseChapterNotFileInfo(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if(paramMap.get("roleId") != null){
				//如果是学生，需要把每个章节是否交了作业以及交作业的评价等
				if(Integer.parseInt(paramMap.get("roleId").toString()) == 1){
					list = chapterDao.getChapterHomeworkListNotFileInfo(paramMap);
					//教师正常获取章节
				}else{
					list = chapterDao.getCourseChapterNotFileInfo(paramMap);
				}
			}else{
				list = chapterDao.getCourseChapterNotFileInfo(paramMap);
			}
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	/**
	 * @description 获取某课程的某个章节章节信息
	 * @param  paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public Map<String, Object> getCourseChapterNotFileInfoByCourseIdAndChpIterd(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> chapterMap = new HashMap<>();
		List<Map<String, Object>> answerList = new ArrayList<Map<String, Object>>();
		try {
			if(paramMap.get("roleId") != null){
				//如果是学生，需要把每个章节是否交了作业以及交作业的评价等
				if(Integer.parseInt(paramMap.get("roleId").toString()) == 1){
					String str = String.valueOf(chapterMap.get("taskAddress"));
					chapterMap = chapterDao.getStudentCourseChapterNotFileInfoByCourseIdAndChpIterd(paramMap);
					List<Map<String, Object>> taskMapList = homeworkTestDao.getStuHomeHistory(paramMap);
					if(taskMapList != null&&taskMapList.size()!=0){
						str = String.valueOf(taskMapList.get(0).get("homeHtml"));
					}
					if(str!=null&&!str.equals("")&&!str.equals("null")){
						//取出学生答案解析成对象后传给前台
						answerList = (List<Map<String, Object>>) JSONObject.parse(str);
						chapterMap.put("answerList",answerList);
					}
					//教师正常获取章节
				}else{
					chapterMap = chapterDao.getTeatherCourseChapterNotFileInfoByCourseIdAndChpIterd(paramMap);
				}
			}else{
				chapterMap = chapterDao.getTeatherCourseChapterNotFileInfoByCourseIdAndChpIterd(paramMap);
			}
		} catch (Exception e) {
			throw e;
		}
		return chapterMap;
	}

	/**
	 * @description 获取某课程的章节信息
	 * @param  paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getCourseChapter(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if(paramMap.get("roleId") != null){
				//如果是学生，需要把每个章节是否交了作业以及交作业的评价等
				if(Integer.parseInt(paramMap.get("roleId").toString()) == 1){
					list = chapterDao.getChapterHomeworkList(paramMap);
					//教师正常获取章节
				}else{
					list = chapterDao.getCourseChapter(paramMap);
				}
			}else{
				list = chapterDao.getCourseChapter(paramMap);
			}
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	/**
	 * @description 修改章节--上半部分
	 * @param  paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public Map<String, Object> saveChapterTop(Map<String, Object> paramMap, HttpServletRequest req) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//查询这个文件名除了这个章节还有没有别的章节叫这个
			/*List<Map<String, Object>> chapterList = chapterDao.getChapterByName(paramMap);
			if(chapterList == null || chapterList.size() == 0){*/
				//通过章节id查询章节基本信
				paramMap.put("coursemark",1);
				Map<String, Object> chapterMap = chapterDao.getOneChapter(paramMap);
				//判断原数据库章节的类型是否与传过来的相同
				if(Integer.parseInt(String.valueOf(chapterMap.get("addTestMark")))!=Integer.parseInt(String.valueOf(paramMap.get("addTestMark")))
						&&Integer.parseInt(String.valueOf(paramMap.get("addTestMark")))!=3&&Integer.parseInt(String.valueOf(chapterMap.get("addTestMark")))!=3){
					//说明类型有改变并且原来的数据类型不是实验+理论类型，现在改成的类型也不是实验+理论类型的，则将原数据库中下半部分清空
					paramMap.put("totalMark",1);
					chapterDao.clearChapterDown(paramMap);
					//删除作业，视频、课件等方法
					courseActionImpl.delHomeworkOrVideo("true", "true", Integer.parseInt(String.valueOf(paramMap.get("chapterId"))), "true", "false","true","true","true");
				}if(Integer.parseInt(String.valueOf(chapterMap.get("addTestMark")))!=Integer.parseInt(String.valueOf(paramMap.get("addTestMark")))
					&&Integer.parseInt(String.valueOf(paramMap.get("addTestMark")))==1&&Integer.parseInt(String.valueOf(chapterMap.get("addTestMark")))==3){
				//说明类型有改变并且原来的数据类型是实验+理论类型，现在改成的类型是实验，则将原数据库中理论相应部分清空
				paramMap.put("totalMark",2);
				chapterDao.clearChapterDown(paramMap);
				//删除作业，视频、课件等方法
				courseActionImpl.delHomeworkOrVideo("true", "false", Integer.parseInt(String.valueOf(paramMap.get("chapterId"))), "true", "false","false","true","false");
			}if(Integer.parseInt(String.valueOf(chapterMap.get("addTestMark")))!=Integer.parseInt(String.valueOf(paramMap.get("addTestMark")))
					&&Integer.parseInt(String.valueOf(paramMap.get("addTestMark")))==2&&Integer.parseInt(String.valueOf(chapterMap.get("addTestMark")))==3){
				//说明类型有改变并且原来的数据类型是实验+理论类型，现在改成的类型是理论，则将原数据库中实验相应部分清空
				paramMap.put("totalMark",3);
				chapterDao.clearChapterDown(paramMap);
				//删除作业，视频、课件等方法
				courseActionImpl.delHomeworkOrVideo("false", "true", Integer.parseInt(String.valueOf(paramMap.get("chapterId"))), "false", "false","true","false","true");
			}
				chapterDao.saveChapter(paramMap);
				resultMap.put("result", true);
				resultMap.put("errMsg", "");
			/*}else{
				resultMap.put("result", false);
				resultMap.put("errMsg", "课节名称已存在");
			}*/
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}


	/***
	 * @description 删除章节的某个文件
	 * @param request  章节id   文件
	 * @param chapterId
	 * @param fileName
	 * 将上传的试验文档，作业，备课材料，答案转换成html
	 */
	public Map<String, Object> delChapterFile(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//删除作业，视频、课件等方法(并将数据库相应的字段清空)
			boolean flag = courseActionImpl.delHomeworkOrVideo(String.valueOf(paramMap.get("delHomework")), String.valueOf(paramMap.get("delVideo")), Integer.parseInt(String.valueOf(paramMap.get("chapterId"))), String.valueOf(paramMap.get("delCourseware")), String.valueOf(paramMap.get("delPackageFile")),String.valueOf(paramMap.get("delExprement")),String.valueOf(paramMap.get("delMaterial")),String.valueOf(paramMap.get("delTest")));
			if(flag){
				resultMap.put("result", "success");
				resultMap.put("failMsg", "");
			}else{
				resultMap.put("result","fail");
				resultMap.put("failMsg","存在学习记录不可删除");
			}


		} catch (Exception e) {
			resultMap.put("result", "fail");
			resultMap.put("failMsg", "删除失败");
			throw e;
		}
		return resultMap;
	}


	/**
	 * @description 修改章节
	 * @param  paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public Map<String, Object> saveChapter(Map<String, Object> paramMap, HttpServletRequest req,CommonsMultipartFile homeworkexcel,CommonsMultipartFile expreexcel) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//查询这个文件名除了这个章节还有没有别的章节叫这个
			List<Map<String, Object>> chapterList = chapterDao.getChapterByName(paramMap);
			if(chapterList == null || chapterList.size() == 0){
				//1025放入录入人id
				paramMap.put("stuorteaId", new SessionValues().getSessionValues(new String[] {"stuorteaId"}, req, "userMsg").get("stuorteaId"));
				chapterDao.saveChapter(paramMap);
				//获取表格中数据
				resultMap = getDate(homeworkexcel,expreexcel,Integer.parseInt(String.valueOf(paramMap.get("chapterId"))));
				if(resultMap==null||resultMap.size()==0){
					resultMap.put("result",true);
				}
			}else{
				resultMap.put("result", false);
				resultMap.put("errMsg", "课节名称已存在");
			}
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}

	/***
	 * @description 修改的时候如果没修改这条文件，就将原来的文件名保存到数据库
	 * @param file 文件流
	 * @param chapterId 章节id
	 * @param columnName 获取的字段
	 * @param newName 上传的文件名
	 * @return 文件名
	 */
	public String getFileName(CommonsMultipartFile file, long chapterId,String columnName, String newName)throws Exception{

		String fileName = "";
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("chapterId", chapterId);
			map.put("oneChapter",1);
			//没上传要原来的
			if(file.getOriginalFilename().equals("")){
				List<Map<String, Object>> mapResult = chapterDao.getCourseChapter(map);
				if(mapResult != null){
					if(mapResult.size() != 0){
						fileName = (String)mapResult.get(0).get(columnName);
					}
				}
				//上传了文件
			}else{
				fileName = newName;
			}
		}catch(Exception e){
			throw e;
		}
		return fileName;
	}



	/**商莹
	 * @description 删除章节中的课件、作业、视频
	 * @param paramMap courseId,chapterNum
	 * @return true成功false失败
	 * @throws Exception
	 */
	public Map<String, Object> delChapterCoHoVo(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> result = new HashMap<String, Object>();
		FileUpload fileUpload = new FileUpload();
		boolean flag = false;
		try{
			int type = Integer.valueOf(String.valueOf(paramMap.get("type")));
			//查询某个章节的对应的章节全部信息
			//Map<String, Object> chapterMsg = chapterDao.getChapterByClassNum(paramMap);
			//删除之前上传的文件（如果没有修改，不删除）
			String[] keysArray = {"server_address", "project_name","tomcatUrl"};
			Map<String, Object> pathMap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String path =(String)pathMap.get("tomcatUrl")+(String)pathMap.get("project_name")+"upload/course/";
			paramMap.put("oneChapter", 1);
			List<Map<String, Object>> chapterList = chapterDao.getCourseChapter(paramMap);
			if(type==1){
				//删除课件
				String coursewareName = (String)chapterList.get(0).get("coursewareName");
				flag = fileUpload.deleteFileOnly(path,coursewareName);
				String coursewareHtml =  (String)chapterList.get(0).get("coursewareNameHtml");
				flag = fileUpload.deleteFileOnly(path,coursewareHtml);
			}if(type==2){
				//删除视频
				String videoName = (String)chapterList.get(0).get("videoName");
				flag = fileUpload.deleteFileOnly(path,videoName);
			}if(type==3){
				//删除作业
				String homeworkName = (String)chapterList.get(0).get("homeworkName");
				String homeworkNameDoc = (String) chapterList.get(0).get("homeworkNameDoc");
				String homeworkNameHtml = (String) chapterList.get(0).get("homeworkNameHtml");
				flag = fileUpload.deleteFileOnly(path,homeworkName);
				flag = fileUpload.deleteFileOnly(path,homeworkNameDoc);
				flag = fileUpload.deleteFileOnly(path,homeworkNameHtml);
			}if(type==5){
				//删除备课材料
				String courseMaterialName = (String)chapterList.get(0).get("courseMaterialName");
				flag = fileUpload.deleteFileOnly(path,courseMaterialName);
				String courseMaterialNameHtml = (String)chapterList.get(0).get("courseMaterialNameHtml");
				flag = fileUpload.deleteFileOnly(path,courseMaterialNameHtml);
			}if(type==6){
				//删除课后答案
				String courseAnswerName = (String)chapterList.get(0).get("courseAnswerName");
				flag = fileUpload.deleteFileOnly(path,courseAnswerName);
				String courseAnswerNameHtml = (String)chapterList.get(0).get("courseAnswerNameHtml");
				flag = fileUpload.deleteFileOnly(path,courseAnswerNameHtml);
			}if(type == 7){
				//删除实验手册
				String testDocName = (String)chapterList.get(0).get("testDocName");
				flag = fileUpload.deleteFileOnly(path,testDocName);
				String testNameHtml = (String)chapterList.get(0).get("testNameHtml");
				flag = fileUpload.deleteFileOnly(path,testNameHtml);
			}if(type == 9){
				//删除课程材料，文件包
				String packageFile =  (String)chapterList.get(0).get("packageFile");
				flag = fileUpload.deleteFileOnly(path,packageFile);
			}
			if(flag){
				//删除章节中的课件、作业、视频（某一个）
				chapterDao.delChapterCoHoVo(paramMap);
			}
			if(flag){
				result.put("result", true);
			}else{
				result.put("result", false);
				result.put("failMsg", "删除失败");
			}
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		return result;
	}

	/**
	 * @description 查询某课程某章节的信息
	 * @param paramMap courseId,chapterId
	 * @return Map
	 * @throws Exception
	 */
	public Map<String, Object> getCourseOneChapter(Map<String, Object> paramMap) throws Exception{

		Map<String, Object> map = new HashMap<String, Object>();
		try{
			map = chapterDao.getCourseOneChapter(paramMap);
		}catch(Exception e){
			throw e;
		}
		return map;
	}

	/**
	 * @description 通过章节名称获取章节信息
	 * @param paramMap Map<String,Object> paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getChapterByName(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = chapterDao.getChapterByName(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return list;
	}

	/**
	 * @description 通过章节名称获取信息，有就返回false没有就返回true
	 * @param chapterName chapterId
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public Boolean getChapterExistMark(String chapterName, long chapterId) throws Exception {

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("chapterName", chapterName);
			map.put("chapterId", chapterId);
			List<Map<String, Object>> list = chapterDao.getChapterByName(map);
			if(list == null || list.size() == 0){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * @description 查询章节列表的方法，分页。
	 * @param  paramMap 存放参数，与数据库字段一致
	 * @return Map<String, Object>  chapterList部门列表，pageNow当前页数，totalPage总页数
	 * @throws Exception
	 */
	public Map<String, Object> getChapterList(Map<String, Object> paramMap, HttpServletRequest req) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
			int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//1025放入参数
			paramMap.put("stuorteaId", new SessionValues().getSessionValues(new String[] {"stuorteaId"}, req, "userMsg").get("stuorteaId"));
			paramMap.put("realRoleId", new SessionValues().getSessionValues(new String[] {"realRoleId"}, req, "userMsg").get("realRoleId"));
			//获取章节信息
			List<Map<String, Object>> chapterList = chapterDao.getChapterList(paramMap);
			returnMap.put("chapterList", chapterList);
			returnMap.put("pageNow", pageNow);
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allChapterList = chapterDao.getChapterList(paramMap);
			int totalPage = pageUtil.getTotalPage(allChapterList, pageSize);
			returnMap.put("totalPage", totalPage);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}


	/**
	 * @description 查询某个章节的详细信息
	 * @param  paramMap chapterId
	 * @return Map<String, Object>
	 * @throws Exception
	 */
	public Map<String, Object> getOneChapter(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			paramMap.put("coursemark",1);//标识调用哪个sql
			returnMap = chapterDao.getOneChapter(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}

	/**
	 * @description 删除章节的方法，传的是每个章节的id。
	 * @param  paramList 部门id集合
	 * @return map中get：result,true成功，false失败
	 *         如果是失败的，从map中get出errorMsg，是失败信息。
	 */
	public Map<String, String> delChapters(List<String> paramList)   {
		Map<String, String> resultMap = new HashMap<String, String>();
		String failMsg = "";
		boolean resultFlag = false;
		//用于存放参数
		Map<String, Object> useMap = null;
		List<Integer> myParamList = new ArrayList<Integer>();
		try {
			for(int position = 0; position < paramList.size(); position++){
				useMap = new HashMap<String, Object>();
				useMap.put("chapterId", Integer.parseInt(paramList.get(position)));
				//查询这个chapter是否与课程有关联
				List<Map<String, Object>> cptList = chapterDao.getMsgByCptId(useMap);
				if(cptList != null && cptList.size() != 0){
					failMsg = cptList.get(0).get("chapterName")+"课节无法删除，已经绑定了课程";
					break;
				}else{
					myParamList.add(Integer.parseInt(paramList.get(position)));
				}
			}
			//都满足条件，全部删除
			if(myParamList.size() == paramList.size()){
				//删除这些章节的文件信息 word与html
				//查询这些章节的所有的课件什么的信息
				List<Map<String, Object>> chapterList = chapterDao.getChapters(paramList);
				//删除文件信息
				DelWorkFileUtil delWorkFileUtil = new DelWorkFileUtil();
				String[] keysArray = {"server_address", "project_name","tomcatUrl"};
				Map<String, Object> pathMap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
				String path =(String)pathMap.get("tomcatUrl")+(String)pathMap.get("project_name")+"upload/course/";
				delWorkFileUtil.delFileList(chapterList,
						new String[]{"coursewareName","coursewareNameHtml",
								"videoName",
								"homeworkName","homeworkNameDoc","homeworkNameHtml",
								"courseMaterialName","courseMaterialNameHtml",
								"courseAnswerName","courseAnswerNameHtml","testDocName","testNameHtml","packageFile"}, path);
				//删除章节信息
				chapterDao.delChapters(myParamList);
				//删除章节与课程的关联信息
				chapterDao.delChapterReCourse(myParamList);
				//删除实验相关部分--调用王晨接口
				courseServiceImpl.deleteImageAllocationByChapterId(myParamList);
				resultFlag = true;
			}
			resultMap.put("result", String.valueOf(resultFlag));
			resultMap.put("errorMsg", failMsg);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "false");
		}
		return resultMap;
	}

	/**
	 * @description 添加作业or实验数据，有就返回false没有就返回true
	 * @param list 数据集合 type 2实验  1作业
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public Boolean addTaskOrExpreLibrary(List<T> list, int type, int chapterId,String homeworkModelName,String courseMark) throws Exception {
		boolean flag =false;
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			map.put("chapterId",chapterId);
			map.put("homeworkModelName",homeworkModelName);
			switch (type){
				case 1:
					//通过章节号查询是否有数据
					List<TaskLibraryVO> taskLibraryList = chapterDao.getTaskLibrary(chapterId);
					if(taskLibraryList!=null&&taskLibraryList.size()!=0){
						//将原有数据删除
						flag = chapterDao.delTaskLibrary(chapterId);
					}
					map.put("taskList",list);
					flag = chapterDao.addTaskLibrary(map);
					//将上传的作业模板的文件名插入数据库
					flag = chapterDao.updateHomeworkModelName(map);
					break;
				case 2:
					//通过章节号查询是否有数据
					List<ExpreLibraryVO> expreLibraryList = chapterDao.getExpreLibrary(chapterId);
					if(expreLibraryList!=null&&expreLibraryList.size()!=0){
						//将原有数据删除
						flag = chapterDao.delExpreLibrary(chapterId);
					}
					map.put("expreList",list);
					flag = chapterDao.addExpreLibrary(map);
					break;
				case 3:
					//两者都上传了
					//通过章节号查询是否有数据
					List<TaskLibraryVO> taskLibraryListTwo = chapterDao.getTaskLibrary(chapterId);
					if(taskLibraryListTwo!=null&&taskLibraryListTwo.size()!=0){
						//将原有数据删除
						flag = chapterDao.delTaskLibrary(chapterId);
					}
					//通过章节号查询是否有数据
					List<ExpreLibraryVO> expreLibraryListTwo = chapterDao.getExpreLibrary(chapterId);
					if(expreLibraryListTwo!=null&&expreLibraryListTwo.size()!=0){
						//将原有数据删除
						flag = chapterDao.delExpreLibrary(chapterId);
					}
					map.put("taskList",list);
					map.put("expreList",list);
					flag = chapterDao.addTaskLibrary(map);
					flag = chapterDao.addExpreLibrary(map);
					break;
				default:break;
			}
		} catch (Exception e) {
			throw e;
		}
		return flag;
	}

	public Map<String,Object> getTaskLibrary(int chapterId){
		Map<String,Object> returnMap = new HashMap<>();
		List<TaskLibraryVO> taskList = null;
		List<TaskLibraryVO> taskOptionList = new ArrayList<>();
		List<TaskLibraryVO> taskManyOptionList = new ArrayList<>();
		List<TaskLibraryVO> taskBlankList = new ArrayList<>();
		List<TaskLibraryVO> taskJudgeList = new ArrayList<>();
		List<TaskLibraryVO> taskAnswerList = new ArrayList<>();
		try {
			taskList = chapterDao.getTaskLibrary(chapterId);
			if (taskList.size()==0){
				returnMap.put("taskOptionList",null);
				returnMap.put("taskManyOptionList",null);
				returnMap.put("taskBlankList",null);
				returnMap.put("taskJudgeList",null);
				returnMap.put("taskAnswerList",null);
			}else {
				for (int i=0;i<taskList.size();i++){
					if(taskList.get(i).getTasklibraryType()==1){
						taskOptionList.add(taskList.get(i));
					}if(taskList.get(i).getTasklibraryType()==2){
						taskJudgeList.add(taskList.get(i));
					}if(taskList.get(i).getTasklibraryType()==3){
						taskBlankList.add(taskList.get(i));
					}if(taskList.get(i).getTasklibraryType()==4){
						taskAnswerList.add(taskList.get(i));
					}if(taskList.get(i).getTasklibraryType()==5){
						taskManyOptionList.add(taskList.get(i));
					}
				}
				returnMap.put("taskOptionList",taskOptionList);
				returnMap.put("taskManyOptionList",taskManyOptionList);
				returnMap.put("taskBlankList",taskBlankList);
				returnMap.put("taskJudgeList",taskJudgeList);
				returnMap.put("taskAnswerList",taskAnswerList);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return returnMap;
	}

	public List<ExpreLibraryVO> getExpreLibrary(int chapterId){
		List<ExpreLibraryVO> expreList = null;
		try {
			expreList = chapterDao.getExpreLibrary(chapterId);
			if(expreList.size()==0){
				expreList=null;
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return expreList;
	}

	/**
	 * @description  作业or实验报告将html代码存入数据库。并将html代码变成网页，转换成word文件，上传到服务器
	 * @param jsonStr  {chapterId，homewokHtmlStr,expreHtmlStr}
	 * @param response
	 */
	public Map<String, String> addHtmlChapter(Map<String,Object> paramMap)   {
		Map<String, String> resultMap = new HashMap<String, String>();
		String failMsg = "";
		boolean resultFlag = false;
		String fileName = "";
		try {
			paramMap.put("coursemark",1);
			Map<String,Object> chapterMap = chapterDao.getOneChapter(paramMap);
			String chapterName = String.valueOf(chapterMap.get("chapterName"));
			//课程标识
			String courseMark = String.valueOf(chapterMap.get("courseMark"));
			//生成的文件名
			String filName = chapterName+courseMark;
			//文件前缀路径
			String[] keysArray = {"server_address", "project_name","tomcatUrl"};
			Map<String, Object> urlmap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String url = String.valueOf(urlmap.get("tomcatUrl"))+String.valueOf(urlmap.get("project_name"))+"upload/course/";
			String stuUrl = String.valueOf(urlmap.get("tomcatUrl"))+String.valueOf(urlmap.get("project_name"))+"upload/stuwork/";
			//测试用的
//			url = "C:\\Users\\Administrator\\Desktop\\xxbg\\";
//			stuUrl = "C:\\Users\\Administrator\\Desktop\\xxbg\\1111\\";
			FileDownloadZipUtil f = new FileDownloadZipUtil();
			f.checkFilderExistsUtil(stuUrl);
			String htmlStr = "";
			String htmlUrl = "";
			String wordUrl = "";
			//将html代码变成html页,并将网页上传到服务器指定位置
			if(Integer.parseInt(String.valueOf(paramMap.get("homeOrExpreMark")))==1){
				String homeworkName = filName+"-"+"课后习题.doc";
				String homeworkNameDoc = filName+"-"+"课后习题.doc";
				String homeworkNameHtml = filName+"-"+"课后习题.html";
				//将前台传过来的html解码
				htmlStr = Escape.unescape(String.valueOf(paramMap.get("homewokHtmlStr")));
				htmlUrl = url+homeworkNameHtml;
				wordUrl = url+homeworkNameDoc;
				stuUrl = stuUrl+homeworkNameHtml;
				//将作业文件名插入chapter表中
				paramMap.put("homeworkName",homeworkName);
				paramMap.put("homeworkNameDoc",homeworkNameDoc);
				paramMap.put("homeworkNameHtml",homeworkNameHtml);
				fileName = homeworkNameHtml;

			}else{
				paramMap.put("homeworkName","");
				paramMap.put("homeworkNameDoc","");
				paramMap.put("homeworkNameHtml","");
			}

			if(Integer.parseInt(String.valueOf(paramMap.get("homeOrExpreMark")))==2){
				paramMap.put("taskType",2);
				List<Map<String, Object>> taskList = chapterDao.getHomeworkListByChapterId(paramMap);
				if(taskList!=null&&taskList.size()!=0){
					resultMap.put("result",String.valueOf("false"));
					resultMap.put("errorMsg","存在学习记录不可修改");
					return resultMap;
				}
				String expreName = filName+"-"+"实验报告.doc";
				String expreNameHtml = filName+"-"+"实验报告.html";
				if(String.valueOf(paramMap.get("expreHtmlStr")).length()>20971520){
					resultMap.put("result",String.valueOf("false"));
					resultMap.put("errorMsg", "实验报告内容长度超限，请精简内容");
					return resultMap;
				}
				//将前台传过来的html解码
				htmlStr = Escape.unescape(String.valueOf(paramMap.get("expreHtmlStr")));
				htmlUrl = url+expreNameHtml;
				wordUrl = url+expreName;
				fileName = expreNameHtml;
				//将作业文件名插入chapter表中
				paramMap.put("expreName",expreName);
				paramMap.put("expreNameHtml",expreNameHtml);
			}else{
				paramMap.put("expreName","");
				paramMap.put("expreNameHtml","");
			}
			//将实验报告或者html转换成html文件
			PubMethods.coverHtml(htmlStr,String.valueOf(paramMap.get("cssStr")),wordUrl,htmlUrl);
			//将实验报告或者html转换成html文件在放到stuwork里一份
			PubMethods.coverHtml(htmlStr,String.valueOf(paramMap.get("cssStr")),wordUrl,stuUrl);
			//将html代码、文件名插入chapter
			resultFlag = chapterDao.addHtml(paramMap);
			if(!resultFlag){
				failMsg = "操作失败";
			}
			resultMap.put("homeworkModelName",String.valueOf(chapterMap.get("homeworkModelName")));
			resultMap.put("result", String.valueOf(resultFlag));
			resultMap.put("errorMsg", failMsg);
			resultMap.put("filename",fileName);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "false");
			resultMap.put("errorMsg", "操作失败");
		}
		return resultMap;
	}

	/**
	 * @description  维护课程--章节详情/修改（可以在线编辑备课材料、实验手册、作业、实验报告）
	 * @param jsonStr  {chapterId，courseMaterialName,testDocName、taskHtml、expreHtml}
	 * @param response
	 */
	public Map<String, String> OnlineEditingChapter(Map<String,Object> paramMap)   {
		Map<String, String> resultMap = new HashMap<String, String>();
		String failMsg = "";
		boolean resultFlag = false;
		try {
			//文件前缀路径
			resultFlag = chapterDao.OnlineEditingChapter(paramMap);
			resultMap.put("result", String.valueOf(resultFlag));
			resultMap.put("errorMsg", failMsg);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "false");
			resultMap.put("errorMsg", "操作失败");
		}
		return resultMap;
	}

	/**@description  修改作业or实验报告时先走这个action判断是否有学习记录
	 * @param request  chapterId、courseId
	 * @return
	 */
	public Map<String, Object> getHistoryStudyByChapIdAndCourseId(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		List<Map<String, Object>> taskList = new ArrayList<>();
		try {
			taskList = chapterDao.getHistoryStudyByChapIdAndCourseId(paramMap);
			if(taskList!=null&&taskList.size()!=0){
				returnMap.put("result","fail");
				returnMap.put("failMsg","存在学习记录不可以修改");
				return returnMap;
			}
			returnMap.put("result","success");
			returnMap.put("failMsg","");
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}

}